package com.woniuxy.patrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.patrol.pojo.PatrolPlan;
import com.woniuxy.patrol.pojo.PatrolPlanRoute;
import com.woniuxy.patrol.service.PatrolPlanService;
import com.woniuxy.patrol.mapper.PatrolPlanMapper;
import com.woniuxy.patrol.mapper.PatrolPlanRouteMapper;
import com.woniuxy.common.utils.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 13162
* @description 针对表【wcm_patrol_plan(巡更计划表)】的数据库操作Service实现
* @createDate 2025-07-14 19:22:37
*/
@Service
public class PatrolPlanServiceImpl extends ServiceImpl<PatrolPlanMapper, PatrolPlan>
    implements PatrolPlanService{

    @Resource
    private PatrolPlanMapper planMapper;
    @Resource
    private PatrolPlanRouteMapper planRouteMapper;

    @Override
    public ResponseUtil getByCommunityId(Long communityId) {
        QueryWrapper<PatrolPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", communityId)
                   .eq("is_delete", 1);
        List<PatrolPlan> plans = planMapper.selectList(queryWrapper);
        return ResponseUtil.success(plans);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseUtil addPlan(PatrolPlan plan, List<Long> routeIds) {
        // 对应《巡更demo.docx》中添加巡更计划并关联路线的功能
        plan.setStatus(1); // 默认为启用
        plan.setIsDelete(1);
        plan.setCreateTime(new Date());
        plan.setUpdateTime(new Date());
        save(plan);

        // 关联路线
        if (routeIds != null && !routeIds.isEmpty()) {
            List<PatrolPlanRoute> relations = new ArrayList<>();
            for (Long routeId : routeIds) {
                PatrolPlanRoute relation = new PatrolPlanRoute();
                relation.setPlanId(plan.getPlanId());
                relation.setRouteId(routeId);
                relation.setIsDelete(1);
                relation.setCreateTime(new Date());
                relation.setUpdateTime(new Date());
                relations.add(relation);
            }
            // 假设PatrolPlanRouteMapper有批量插入方法
            planRouteMapper.batchInsert(relations);
        }

        return ResponseUtil.success("巡更计划添加成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseUtil updatePlan(PatrolPlan plan, List<Long> routeIds) {
        plan.setUpdateTime(new Date());
        updateById(plan);

        // 先删除原有关联路线
        QueryWrapper<PatrolPlanRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", plan.getPlanId());
        planRouteMapper.delete(queryWrapper);

        // 添加新关联
        if (routeIds != null && !routeIds.isEmpty()) {
            List<PatrolPlanRoute> relations = new ArrayList<>();
            for (Long routeId : routeIds) {
                PatrolPlanRoute relation = new PatrolPlanRoute();
                relation.setPlanId(plan.getPlanId());
                relation.setRouteId(routeId);
                relation.setIsDelete(1);
                relation.setCreateTime(new Date());
                relation.setUpdateTime(new Date());
                relations.add(relation);
            }
            planRouteMapper.batchInsert(relations);
        }

        return ResponseUtil.success("巡更计划更新成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseUtil deletePlan(Long planId) {
        // 逻辑删除计划
        PatrolPlan plan = new PatrolPlan();
        plan.setPlanId(planId);
        plan.setIsDelete(0);
        plan.setUpdateTime(new Date());
        updateById(plan);

        // 删除关联路线
        QueryWrapper<PatrolPlanRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", planId);
        planRouteMapper.delete(queryWrapper);
        return ResponseUtil.success("巡更计划删除成功");
    }
}